<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "article_to".
 *
 * @property string $id
 * @property string $article_id
 * @property string $type
 * @property string $pk
 */
class ArticleTo extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'article_to';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['article_id', 'type', 'pk'], 'required'],
            [['article_id', 'pk'], 'integer'],
            [['type'], 'string', 'max' => 50],
            [['article_id', 'type', 'pk'], 'unique', 'targetAttribute' => ['article_id', 'type', 'pk'], 'message' => 'The combination of Article ID, 类型：如catalog,post and 类型id has already been taken.'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'article_id' => 'Article ID',
            'type' => '类型：如catalog,post',
            'pk' => '类型id',
        ];
    }

    /**
     * 返回ArticleTo对象
     * @param $type
     * @param $pk
     * @return array|ArticleTo|null|\yii\db\ActiveRecord
     */
    public static function get($type, $pk)
    {
        $model = ArticleTo::find()->where("type=:type and pk=:pk", [":type" => $type, ":pk" => $pk])->one();
        if (!$model) {
            $model = new ArticleTo();
        }
        return $model;
    }

    /**
     * 保存
     * @param $type string 类型
     * @param $pk int 类型id
     */
    public static function log($article_id, $type, $pk)
    {

        $model = ArticleTo::get($type, $pk);
        $model->article_id = $article_id;
        $model->type = $type;
        $model->pk = $pk;
        $model->save();
    }

    public static function del($type, $pk)
    {
        ArticleTo::get($type, $pk)->delete();
    }

    /**
     * 获取素材
     * @param $article_id
     * @return static
     */
    public static function getArticle($article_id)
    {
        return Article::findOne($article_id);
    }

    /**
     * 获取素材ID和标题
     * @param $article_id
     * @return array
     */
    public static function getArticleInfo($article_id)
    {
        $article = ArticleTo::getArticle($article_id);
        if ($article) {
            return ['id' => $article->id, 'title' => $article->title];
        }
        return ['id' => '', 'title' => ''];
    }

    /**
     * 获取素材标题
     * @param $article_id
     * @return mixed|null
     */
    public static function getArticleTitle($article_id)
    {
        $article = ArticleTo::getArticle($article_id);
        if ($article) {
            return $article->title;
        }
        return null;
    }

    /**
     * 获取素材id
     * @param $article_id
     * @return mixed|null
     */
    public static function getArticleId($article_id)
    {
        $article = ArticleTo::getArticle($article_id);
        if ($article) {
            return $article->id;
        }
        return null;
    }

    /**
     * 获取素材
     * @param $type
     * @param $pk
     * @return array|ArticleTo
     */
    public static function getArticleByType($type, $pk)
    {
        $model = ArticleTo::get($type, $pk);
        if ($model) {
            return ArticleTo::getArticle($model->article_id);
        }
        return [];
    }

    /**
     * 获取素材ID和标题
     * @param $type
     * @param $pk
     * @return array|ArticleTo
     */
    public static function getArticleInfoByType($type, $pk)
    {
        $model = ArticleTo::get($type, $pk);
        if ($model) {
            return ArticleTo::getArticleInfo($model->article_id);
        }
        return ['id' => '', 'title' => ''];
    }
}
